package com.ai.cltcore.util;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

import com.ai.frame.logger.Logger;
import com.ai.frame.logger.LoggerFactory;


/**
 * Parsing The Configuration File
 */
public final class PropertiesUtil extends PropertyPlaceholderConfigurer{
	
	private static final Logger LOGGER = LoggerFactory.getApplicationLog(PropertiesUtil.class);
	private static Map<String, String> ctxPropertiesMap  = new HashMap<>();
	
	protected void loadProperties(Properties props) throws IOException {
		super.loadProperties(props);
		try {
			String password = props.getProperty("password");
			String password_41 = props.getProperty("password_41");
			String password_43 = props.getProperty("password_43");
			String password_51 = props.getProperty("password_51");
			String password_53 = props.getProperty("password_53");
			String password_61 = props.getProperty("password_61");
			String decryPassword = new String(
					EncryptionUtil.decode(EncryptionUtil.hex2byte(StringUtil.isEmpty(password)?"":password), "asiainfo".getBytes()));
			String decryPassword_41 = new String(
					EncryptionUtil.decode(EncryptionUtil.hex2byte(StringUtil.isEmpty(password_41)?"":password_41), "asiainfo".getBytes()));
			String decryPassword_43 = new String(
					EncryptionUtil.decode(EncryptionUtil.hex2byte(StringUtil.isEmpty(password_43)?"":password_43), "asiainfo".getBytes()));
			String decryPassword_51 = new String(
					EncryptionUtil.decode(EncryptionUtil.hex2byte(StringUtil.isEmpty(password_51)?"":password_51), "asiainfo".getBytes()));
			String decryPassword_53 = new String(
					EncryptionUtil.decode(EncryptionUtil.hex2byte(StringUtil.isEmpty(password_53)?"":password_53), "asiainfo".getBytes()));
			String decryPassword_61 = new String(
					EncryptionUtil.decode(EncryptionUtil.hex2byte(StringUtil.isEmpty(password_61)?"":password_61), "asiainfo".getBytes()));
			props.setProperty("password", decryPassword);
			props.setProperty("password_41", decryPassword_41);
			props.setProperty("password_43", decryPassword_43);
			props.setProperty("password_51", decryPassword_51);
			props.setProperty("password_53", decryPassword_53);
			props.setProperty("password_61", decryPassword_61);
		} catch (Exception e) {
			LOGGER.error("error","decode password in properties error!", e);
		}
	}

	@Override
	protected void processProperties(
			ConfigurableListableBeanFactory beanFactoryToProcess,
			Properties props) throws BeansException {
		super.processProperties(beanFactoryToProcess, props);
		
		for (Object key : props.keySet()) {
			String keyStr = key.toString();
			String value = props.getProperty(keyStr);
			ctxPropertiesMap.put(keyStr, value);
		}
	}


	/**
	 * Get a value based on key , if key does not exist , null is returned
	 * 
	 * @param key
	 * @return
	 */
	public static String getString(String key) {
		try {
			return ctxPropertiesMap.get(key);
		} catch (Exception e) {
			LOGGER.info("","",e);
			return null;
		}
	}

	/**
	 * 根据key获取值
	 * 
	 * @param key
	 * @return
	 */
	public static int getInt(String key) {
		return Integer.parseInt(ctxPropertiesMap.get(key));
	}
}